package com.zx._10_设计模式.结构型模式.代理模式;

import com.zx._10_设计模式.结构型模式.装饰者模式.BaseClass;
import com.zx._10_设计模式.结构型模式.装饰者模式.CommonClass;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler<T> implements InvocationHandler {

    private T t;

    public ProxyInvocationHandler(T t) {
        this.t = t;
    }

    public static void main(String[] args) {
        CommonClass obj = (CommonClass) Proxy.newProxyInstance(ProxyInvocationHandler.class.getClassLoader(),
                CommonClass.class.getInterfaces(),
                new ProxyInvocationHandler<CommonClass>(new BaseClass()));
        obj.hello();

    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理start");
        Object invoke = method.invoke(t, args);
        System.out.println("代理end");
        return invoke;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

}
